// function SpeedLevel(level, speed, score) {
//     this.level = level;
//     this.speed = speed;
//     this.score = score;
// }

// function EnemyGroups(id, des, time) {
//     this.id = id;
//     this.des = des;
//     this.time = time;
// }

var dataMgr = {
    //当前版本名称
    versionName: "v1.0",
    //当前版本号
    versionCode: 1,
    //玩家信息
    roleinfo: {
        //子弹速度等级信息
        speedLevel: [],
        //子弹攻击力量等级信息
        powerLevel: []
    },
    //增强速度道具
    speedProp: {
        des: "",
        //增强速度倍数
        speedTimes: 2,
        //增强速度持续时间
        keepTime: 2,
        //随机出现概率
        rand: 50
    },
    //增强攻击力道具
    powerProp: {
        des: "",
        //攻击力增强倍数
        powerTimes: 2,
        //持续时间
        keepTime: 2,
        //随机出现概率
        rand: 50
    },
    //三条方向子弹道具
    multButProp: {
        des: "",
        //左右两条方向角度
        angleDev: 5,
        //持续时间
        keepTime: 2,
        //随机出现概率
        rand: 50
    },
    //方块单元模型数组
    enemyGroups: [],
    //游戏进度配置
    games: {
        vedioPassTime:6,
        vedioAddGold:100,//看视频添加金币
        scoreToGold:1,//每局分数转化成金币
        //方块单元相邻出现的时间间隔
        cellDelay: 0,
        //方块单元向下移动的速度
        cellMoveSpeed: 0,
        //子弹移动速度
        bulletMoveSpeed: 0,
        //子弹射出的速度*子弹力量 对应数字的系数  即数字最小值为 速度*力量*minRatio 在game.json里面配置
        minRatio: 0,
        //子弹射出的速度*子弹力量 对应数字的系数  即数字最大值为 速度*力量*minRatio 在game.json里面配置
        maxRatio: 2,
        //每经过一个方块单元最小和最大的系数增加值
        addRatio: 0.01,
        //第一波方块单元配置，游戏初期一般配置为较为简单的1和2两种单元
        firstTimes: {
            des: "",
            //第一波最小次数
            minTimes: 0,
            //第一波最大次数
            maxTimes: 0,
            //第一波需要出现哪些方块，数组为需要出现的方块id
            enemys: []
        },
        //游戏进行中的方块单元数组
        randEnemys: [],
        //游戏进行中的随机方块单元概率，与上面对应
        randNum: [],
        //游戏进行中每经过一个单元概率的向后递增值，增加游戏难度。数值越大，游戏难度的变化越快
        change1Sec: 0
    },
    //以上部分是从json里面解析的模板内容，以下内容为英雄的当前属性
    roleSpeedLevel: 1,//速度等级，本地保存
    rolePowerLevel: 1,//伤害等级
    curRoleSpeedInfo: null,//当前子弹的速度信息
    curRolePowerInfo: null,//当前子弹的攻击力量信息
    roleGold: 0,//剩余分数
    maxScore:0,//历史最高分数
    lastScore:0,//上局分数
    isPlaySound: true,//是否播放音效

    parseData: function (json) {
        this.versionName = json.versionName;
        this.versionCode = json.versionCode;
        this.roleinfo.speedLevel = json.roleinfo.speedLevel;
        this.roleinfo.powerLevel = json.roleinfo.powerLevel;
        // for(let i=0;i<role.length;i++){
        //    let lv=role[i].level;
        //    let sp=role[i].speed;
        //    let sc=role[i].score;
        //    this.roleinfo.speedLevel[i]=new SpeedLevel(lv,sp,sc);
        // }
        this.speedProp.speedTimes = json.speedProp.speedTimes;
        this.speedProp.des = json.speedProp.des;
        this.speedProp.keepTime=json.speedProp.keepTime;
        this.speedProp.rand=json.speedProp.rand;

        this.powerProp.speedTimes = json.powerProp.speedTimes;
        this.powerProp.des = json.powerProp.des;
        this.powerProp.keepTime=json.powerProp.keepTime;
        this.powerProp.rand=json.powerProp.rand;

        this.multButProp.angleDev = json.multButProp.angleDev;
        this.multButProp.des = json.multButProp.des;
        this.multButProp.keepTime=json.multButProp.keepTime;
        this.multButProp.rand=json.multButProp.rand;

        this.enemyGroups = json.enemyGroups;
        // for(let i=0;i<enemys.length;i++){
        //     let id=enemys[i].id;
        //     let des=enemys[i].des;
        //     let time=enemys[i].time;
        //     this.enemyGroups[i]=new EnemyGroups(id,des,time);
        // }
        this.games.vedioPassTime=json.games.vedioPassTime;
        this.games.vedioAddGold=json.games.vedioAddGold;
        this.games.scoreToGold=json.games.scoreToGold;
        this.games.cellDelay = json.games.cellDelay;
        this.games.cellMoveSpeed = json.games.cellMoveSpeed;
        this.games.cellStarPercent = json.games.cellStarPercent
        this.games.bulletMoveSpeed = json.games.bulletMoveSpeed;
        this.games.minRatio = json.games.minRatio;
        this.games.maxRatio = json.games.maxRatio;
        this.games.addRatio = json.games.addRatio;
        this.games.firstTimes.des = json.games.firstTimes.des;
        this.games.firstTimes.minTimes = json.games.firstTimes.minTimes;
        this.games.firstTimes.maxTimes = json.games.firstTimes.maxTimes;
        this.games.firstTimes.enemys = json.games.firstTimes.enemys;
        this.games.randEnemys = json.games.randEnemys;
        this.games.randNum = json.games.randNum;
        this.games.change1Sec = json.games.change1Sec;

        //解析完成后再赋值
        this.curRoleSpeedInfo = this.getCurRoleSpeedInfo();
        this.curRolePowerInfo = this.getCurRolePowerInfo();
    },  

    initData: function (callback) {
        let save = cc.sys.localStorage;
        let speedLevel = save.getItem("speed_level");
        if (speedLevel) {
            this.roleSpeedLevel = parseInt(speedLevel);
        }
        let powerLevel = save.getItem("power_level");
        if (powerLevel) {
            this.rolePowerLevel = parseInt(powerLevel);
        }
        let gold = save.getItem("gold");
        if (gold) {
            this.roleGold = parseInt(gold);
        }
        let max=save.getItem("maxScore");
        if(max){
            this.maxScore= parseInt(max);
        }
        let last=save.getItem("lastScore");
        if(last){
            this.lastScore=parseInt(last);
        }
        cc.loader.loadRes("game", function (err, data) {
            if (err) {
                cc.log(err);
            } else {
                let list = data;
                this.parseData(list);
                callback();
            }
        }.bind(this));
    },
    getCurRoleSpeedInfo: function () {
        if (this.roleSpeedLevel > 0) {
            for (let i = 0; i < this.roleinfo.speedLevel.length; i++) {
                let tmp = this.roleinfo.speedLevel[i];
                if (tmp.level == this.roleSpeedLevel) {
                    return tmp;
                }
            }
        }
        return 0;
    },
    getCurRolePowerInfo: function () {
        if (this.rolePowerLevel > 0) {
            for (let i = 0; i < this.roleinfo.powerLevel.length; i++) {
                let tmp = this.roleinfo.powerLevel[i];
                if (tmp.level == this.rolePowerLevel) {
                    return tmp;
                }
            }
        }
        return 0;
    },
    setRoleSpeedLevel: function (level) {
        this.roleSpeedLevel = level;
        this.curRoleSpeedInfo = this.getCurRoleSpeedInfo();
        cc.sys.localStorage.setItem("speed_level", level);

    },
    setRolePowerLevel: function (level) {
        this.rolePowerLevel = level;
        this.curRolePowerInfo = this.getCurRolePowerInfo();
        cc.sys.localStorage.setItem("power_level", level);
    },

    setRoleGold: function (gold) {
        this.roleGold = gold;
        cc.sys.localStorage.setItem("gold", this.roleGold);
    },

    addRoleGold: function (gold) {
        this.roleGold += gold;
        this.setRoleGold(this.roleGold);
    },

    setMaxScore:function(score){
        this.maxScore=score;
        cc.sys.localStorage.setItem("maxScore", this.maxScore);
    },

    setLastScore:function(score){
        this.lastScore=score;
        cc.sys.localStorage.setItem("lastScore", this.lastScore);
        if(score>this.maxScore){
            this.setMaxScore(score);
        }
    },

    upSpeedLevel: function () {
        if (this.roleSpeedLevel >= this.roleinfo.speedLevel.length) {
            //已经升到了最高等级
            return false;
        }
        //升级等级
        if (this.roleGold < this.curRoleSpeedInfo.gold) {
            //金币不够
            return false;
        }
        let gold = this.roleGold - this.curRoleSpeedInfo.gold;
        this.setRoleSpeedLevel(this.roleSpeedLevel + 1);
        this.setRoleGold(gold);
        return true;
    },
    upPowerLevel: function () {
        if (this.rolePowerLevel >= this.roleinfo.powerLevel.length) {
            //已经升到了最高等级
            return false;
        }
        //升级等级
        if (this.roleGold < this.curRolePowerInfo.gold) {
            //金币不够
            return false;
        }
        let gold = this.roleGold - this.curRolePowerInfo.gold;
        this.setRolePowerLevel(this.rolePowerLevel + 1);
        this.setRoleGold(gold);
        return true;
    }
};
module.exports = dataMgr;